import os
import cv2

# Define the input and output directories
input_dir = 'C:\\Users\\jxk33\\Desktop\\raw_images'
output_dir = 'C:\\Users\\jxk33\\Desktop\\images2'

# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# Process each PNG image in the input directory
for filename in os.listdir(input_dir):
    if filename.endswith('.png'):
        # Construct the full file path
        img_path = os.path.join(input_dir, filename)

        # Read the image
        image = cv2.imread(img_path)

        # Check if the image was loaded successfully
        if image is None:
            print(f"Error loading image: {img_path}")
            continue

        # Convert the image to grayscale
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # Apply binary thresholding
        _, binary_image = cv2.threshold(gray_image, 120, 255, cv2.THRESH_BINARY)

        # Construct the output file path
        output_path = os.path.join(output_dir, filename)

        # Save the processed image
        cv2.imwrite(output_path, binary_image)

        print(f"Processed and saved: {output_path}")

print("Processing complete.")
